﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;

namespace ZHDJ.AppInterface.Controllers
{
    public class MessageBoardController : ApiController
    {
        #region 上传图片
        [HttpPost]
        public ReturnResult UpImage(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            string image = Convert.ToString(obj.imgurl);//Base64字符编码图片
            string Extension = Convert.ToString(obj.Extension);//扩展名

            ImageModel imageModel = InterfaceHelper.UpImge(image, Extension);
            if (imageModel != null)
            {
                model.code = "200";
                model.result = imageModel;
                model.msg = "上传成功！";
            }
            else
            {
                model.code = "201";
                model.msg = "上传失败！";
            }

            return model;
        }
        #endregion

        #region 获取所有组织
        [HttpPost]
        public ReturnResult GetAllBranch()
        {
            DataTable dt = InterfaceHelper.GetAllBranch();
            IList<PartyOrgBranch> list = DataTableToListModel<PartyOrgBranch>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = ""
            };
        }
        #endregion

        #region 根据条件获取组织
        [HttpPost]
        public ReturnResult GetBranchListWhereBranchName(dynamic obj)
        {
            string branchName = Convert.ToString(obj.keyword);
            DataTable dt = InterfaceHelper.GetBranchListWhereBranchName(branchName);
            IList<PartyOrgBranch> list = DataTableToListModel<PartyOrgBranch>.ConvertToModel(dt);
            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = ""
            };
        }
        #endregion

        #region  根据ParentBranchID获取组织
        [HttpPost]
        public ReturnResult GetBranchListWhereParentBranchID(dynamic obj)
        {
            string ParentBranchID = Convert.ToString(obj.ParentBranchID);
            DataTable dt = InterfaceHelper.GetBranchListWhereParentBranchID(ParentBranchID);
            IList<PartyOrgBranch> list = DataTableToListModel<PartyOrgBranch>.ConvertToModel(dt);
            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = ""
            };
        }
        #endregion

        #region 留言板保存方法
        [HttpPost]
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            PartyMessageBoard message = new PartyMessageBoard();

            try
            {
                message.ID = StringHelper.NewID;
                message.UserName = Convert.ToString(obj.Name);
                message.MobileNo = Convert.ToString(obj.MobileNo);
                message.BranchID = Convert.ToString(obj.branchID);
                message.Message = Convert.ToString(obj.content);
                message.CreateTime = DateTime.Now;
                message.CreateBy = Convert.ToString(obj.CreateBy);
                //message.Image = Convert.ToString(obj.ImageUrl);
                message.Title = Convert.ToString(obj.Title);
                InterfaceHelper.MessageBoardSave(message);
                //批量添加文件表
                string[] array = Convert.ToString(obj.imgString).TrimEnd(',').Split(',');
                for (int i = 0; i < array.Length; i++)
                {
                    PartyFile file = new PartyFile();
                    file.ID = StringHelper.NewID;
                    file.FID = message.ID;
                    file.Title = message.Title + i.ToString();
                    file.Extension = ".jpg";
                    file.Description = message.Title + i.ToString();
                    file.Size = 0;
                    file.Url = array[i];
                    file.CreateTime = DateTime.Now;
                    file.CreateBy = message.CreateBy;
                    file.ModifyTime = DateTime.Now;
                    file.ModifyBy = message.CreateBy;
                    InterfaceHelper.AddFileTalbe(file);
                }
                model.code = "200";
                model.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                model.code = "201";
                model.msg = "保存失败！";
            }

            return model;
        }
        #endregion

        #region 获取留言板数据
        [HttpPost]
        public ReturnResult GetMessageBoard(dynamic obj)
        {
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string CreateBy = Convert.ToString(obj.CreateBy);
            DataTable dt = InterfaceHelper.GetMessageBoard(pageIndex, pageSize, CreateBy);
            IList<PartyMessageBoard> list = new List<PartyMessageBoard>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyMessageBoard>.ConvertToModel(dt);

            foreach (PartyMessageBoard model in list)
            {
                DataTable dtfile = InterfaceHelper.GetPartyFileByFID(model.ID);
                List<string> newlist = new List<string>();
                foreach (DataRow item in dtfile.Rows)
                {
                    newlist.Add(item["Url"].ToString());
                }
                model.PicList = newlist;
            }

            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 获取留言板实体
        [HttpPost]
        public ReturnResult GetMessageBoardModel(dynamic obj)
        {
            string ID = Convert.ToString(obj.ID);
            PartyMessageBoard model = InterfaceHelper.GetMessageBoardModel(ID);
            return new ReturnResult()
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion
    }
}
